﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;

namespace CentrisScraper.Lib
{
    public static class ListingScraper
    {
        public static IList<Listing> GetListingsForVentes(DateTime date)
        {
            string strDate = date.Month + "/" + date.Day + "/" + date.Year;
            string[] requests = 
            { 
                // Ahuntsic
                "{\"maximumResults\":100,\"searchCriteria\":{\"Source\":\"FCIQ\",\"CurrentTab\":\"List\",\"EncryptID\":\"ZhvgOE2Vwqs=\",\"iMapZoom\":15,\"MapCoord\":\"45.573317172628016, -73.68002414703369,45.54411014853389, -73.64084243774414\",\"County\":\"%GS4621\",\"Address\":\"\",\"Area\":\"\",\"MLSArea\":\"\",\"School\":\"\",\"City\":\"\",\"Neighborhood\":\"\",\"State\":\"\",\"Zipcode\":\"\",\"SrchType\":\"\",\"PropType\":\"RES\",\"iMinPrice\":0,\"iMaxPrice\":600000,\"iBeds\":3,\"iBaths\":0,\"iBathsFull\":0,\"iBathsPartial\":0,\"iNumStories\":0,\"iSqFtMin\":0,\"iSqFtMax\":0,\"fAcreMin\":0,\"fAcreMax\":0,\"iYrBuiltMin\":0,\"iYrBuiltMax\":0,\"Pool\":\"\",\"Waterfront\":\"\",\"Waterview\":\"\",\"ShortSaleREO\":\"\",\"iGarage\":0,\"Search1\":\"\",\"Search2\":\"\",\"Search3\":\"\",\"Search4\":\"\",\"Search5\":\"\",\"Search6\":\"\",\"Search7\":\"\",\"Search8\":\"\",\"AllRemarks\":\"\",\"AllFeatures\":\"\",\"OpenHouse\":\"\",\"HasVirtualTour\":\"\",\"HasPhoto\":\"\",\"Status\":\"A\",\"isRentalOnlySearch\":\"N\",\"UserHomeMLS\":\"FCIQ\",\"Priority\":\"Price+\",\"AgentID\":\"\",\"isQuickCountable\":false,\"ListedSinceDate\":\"" + strDate + "\"},\"isSavedSearchRunning\":false}",
                "{\"maximumResults\":100,\"searchCriteria\":{\"Source\":\"FCIQ\",\"CurrentTab\":\"List\",\"EncryptID\":\"ZhvgOE2Vwqs=\",\"iMapZoom\":15,\"MapCoord\":\"45.573317172628016, -73.68002414703369,45.54411014853389, -73.64084243774414\",\"County\":\"%GS4621\",\"Address\":\"\",\"Area\":\"\",\"MLSArea\":\"\",\"School\":\"\",\"City\":\"\",\"Neighborhood\":\"\",\"State\":\"\",\"Zipcode\":\"\",\"SrchType\":\"\",\"PropType\":\"PPR\",\"iMinPrice\":0,\"iMaxPrice\":700000,\"iBeds\":3,\"iBaths\":0,\"iBathsFull\":0,\"iBathsPartial\":0,\"iNumStories\":0,\"iSqFtMin\":0,\"iSqFtMax\":0,\"fAcreMin\":0,\"fAcreMax\":0,\"iYrBuiltMin\":0,\"iYrBuiltMax\":0,\"Pool\":\"\",\"Waterfront\":\"\",\"Waterview\":\"\",\"ShortSaleREO\":\"\",\"iGarage\":0,\"Search1\":\"\",\"Search2\":\"=2X\",\"Search3\":\"\",\"Search4\":\"\",\"Search5\":\"\",\"Search6\":\"\",\"Search7\":\"\",\"Search8\":\"\",\"AllRemarks\":\"\",\"AllFeatures\":\"\",\"OpenHouse\":\"\",\"HasVirtualTour\":\"\",\"HasPhoto\":\"\",\"Status\":\"A\",\"isRentalOnlySearch\":\"N\",\"UserHomeMLS\":\"FCIQ\",\"Priority\":\"Price+\",\"AgentID\":\"\",\"isQuickCountable\":false,\"ListedSinceDate\":\"" + strDate + "\"},\"isSavedSearchRunning\":false}",
                "{\"maximumResults\":100,\"searchCriteria\":{\"Source\":\"FCIQ\",\"CurrentTab\":\"List\",\"EncryptID\":\"ZhvgOE2Vwqs=\",\"iMapZoom\":15,\"MapCoord\":\"45.573317172628016, -73.68002414703369,45.54411014853389, -73.64084243774414\",\"County\":\"%GS4621\",\"Address\":\"\",\"Area\":\"\",\"MLSArea\":\"\",\"School\":\"\",\"City\":\"\",\"Neighborhood\":\"\",\"State\":\"\",\"Zipcode\":\"\",\"SrchType\":\"\",\"PropType\":\"PPR\",\"iMinPrice\":0,\"iMaxPrice\":800000,\"iBeds\":3,\"iBaths\":0,\"iBathsFull\":0,\"iBathsPartial\":0,\"iNumStories\":0,\"iSqFtMin\":0,\"iSqFtMax\":0,\"fAcreMin\":0,\"fAcreMax\":0,\"iYrBuiltMin\":0,\"iYrBuiltMax\":0,\"Pool\":\"\",\"Waterfront\":\"\",\"Waterview\":\"\",\"ShortSaleREO\":\"\",\"iGarage\":0,\"Search1\":\"\",\"Search2\":\"=3X\",\"Search3\":\"\",\"Search4\":\"\",\"Search5\":\"\",\"Search6\":\"\",\"Search7\":\"\",\"Search8\":\"\",\"AllRemarks\":\"\",\"AllFeatures\":\"\",\"OpenHouse\":\"\",\"HasVirtualTour\":\"\",\"HasPhoto\":\"\",\"Status\":\"A\",\"isRentalOnlySearch\":\"N\",\"UserHomeMLS\":\"FCIQ\",\"Priority\":\"Price+\",\"AgentID\":\"\",\"isQuickCountable\":false,\"ListedSinceDate\":\"" + strDate + "\"},\"isSavedSearchRunning\":false}",
                // St-Laurent
                "{\"maximumResults\":100,\"searchCriteria\":{\"Source\":\"FCIQ\",\"CurrentTab\":\"List\",\"EncryptID\":\"ZhvgOE2Vwqs=\",\"iMapZoom\":15,\"MapCoord\":\"45.5295009432847, -73.70465755462646,45.5002711462007, -73.66547584533693\",\"County\":\"%GS4621\",\"Address\":\"\",\"Area\":\"\",\"MLSArea\":\"\",\"School\":\"\",\"City\":\"\",\"Neighborhood\":\"\",\"State\":\"\",\"Zipcode\":\"\",\"SrchType\":\"\",\"PropType\":\"RES\",\"iMinPrice\":0,\"iMaxPrice\":600000,\"iBeds\":3,\"iBaths\":0,\"iBathsFull\":0,\"iBathsPartial\":0,\"iNumStories\":0,\"iSqFtMin\":0,\"iSqFtMax\":0,\"fAcreMin\":0,\"fAcreMax\":0,\"iYrBuiltMin\":0,\"iYrBuiltMax\":0,\"Pool\":\"\",\"Waterfront\":\"\",\"Waterview\":\"\",\"ShortSaleREO\":\"\",\"iGarage\":0,\"Search1\":\"\",\"Search2\":\"\",\"Search3\":\"\",\"Search4\":\"\",\"Search5\":\"\",\"Search6\":\"\",\"Search7\":\"\",\"Search8\":\"\",\"AllRemarks\":\"\",\"AllFeatures\":\"\",\"OpenHouse\":\"\",\"HasVirtualTour\":\"\",\"HasPhoto\":\"\",\"Status\":\"A\",\"isRentalOnlySearch\":\"N\",\"UserHomeMLS\":\"FCIQ\",\"Priority\":\"Price+\",\"AgentID\":\"\",\"isQuickCountable\":false,\"ListedSinceDate\":\"" + strDate + "\"},\"isSavedSearchRunning\":false}",
                "{\"maximumResults\":100,\"searchCriteria\":{\"Source\":\"FCIQ\",\"CurrentTab\":\"List\",\"EncryptID\":\"ZhvgOE2Vwqs=\",\"iMapZoom\":15,\"MapCoord\":\"45.5295009432847, -73.70465755462646,45.5002711462007, -73.66547584533693\",\"County\":\"%GS4621\",\"Address\":\"\",\"Area\":\"\",\"MLSArea\":\"\",\"School\":\"\",\"City\":\"\",\"Neighborhood\":\"\",\"State\":\"\",\"Zipcode\":\"\",\"SrchType\":\"\",\"PropType\":\"PPR\",\"iMinPrice\":0,\"iMaxPrice\":700000,\"iBeds\":3,\"iBaths\":0,\"iBathsFull\":0,\"iBathsPartial\":0,\"iNumStories\":0,\"iSqFtMin\":0,\"iSqFtMax\":0,\"fAcreMin\":0,\"fAcreMax\":0,\"iYrBuiltMin\":0,\"iYrBuiltMax\":0,\"Pool\":\"\",\"Waterfront\":\"\",\"Waterview\":\"\",\"ShortSaleREO\":\"\",\"iGarage\":0,\"Search1\":\"\",\"Search2\":\"=2X\",\"Search3\":\"\",\"Search4\":\"\",\"Search5\":\"\",\"Search6\":\"\",\"Search7\":\"\",\"Search8\":\"\",\"AllRemarks\":\"\",\"AllFeatures\":\"\",\"OpenHouse\":\"\",\"HasVirtualTour\":\"\",\"HasPhoto\":\"\",\"Status\":\"A\",\"isRentalOnlySearch\":\"N\",\"UserHomeMLS\":\"FCIQ\",\"Priority\":\"Price+\",\"AgentID\":\"\",\"isQuickCountable\":false,\"ListedSinceDate\":\"" + strDate + "\"},\"isSavedSearchRunning\":false}",
                "{\"maximumResults\":100,\"searchCriteria\":{\"Source\":\"FCIQ\",\"CurrentTab\":\"List\",\"EncryptID\":\"ZhvgOE2Vwqs=\",\"iMapZoom\":15,\"MapCoord\":\"45.5295009432847, -73.70465755462646,45.5002711462007, -73.66547584533693\",\"County\":\"%GS4621\",\"Address\":\"\",\"Area\":\"\",\"MLSArea\":\"\",\"School\":\"\",\"City\":\"\",\"Neighborhood\":\"\",\"State\":\"\",\"Zipcode\":\"\",\"SrchType\":\"\",\"PropType\":\"PPR\",\"iMinPrice\":0,\"iMaxPrice\":800000,\"iBeds\":3,\"iBaths\":0,\"iBathsFull\":0,\"iBathsPartial\":0,\"iNumStories\":0,\"iSqFtMin\":0,\"iSqFtMax\":0,\"fAcreMin\":0,\"fAcreMax\":0,\"iYrBuiltMin\":0,\"iYrBuiltMax\":0,\"Pool\":\"\",\"Waterfront\":\"\",\"Waterview\":\"\",\"ShortSaleREO\":\"\",\"iGarage\":0,\"Search1\":\"\",\"Search2\":\"=3X\",\"Search3\":\"\",\"Search4\":\"\",\"Search5\":\"\",\"Search6\":\"\",\"Search7\":\"\",\"Search8\":\"\",\"AllRemarks\":\"\",\"AllFeatures\":\"\",\"OpenHouse\":\"\",\"HasVirtualTour\":\"\",\"HasPhoto\":\"\",\"Status\":\"A\",\"isRentalOnlySearch\":\"N\",\"UserHomeMLS\":\"FCIQ\",\"Priority\":\"Price+\",\"AgentID\":\"\",\"isQuickCountable\":false,\"ListedSinceDate\":\"" + strDate + "\"},\"isSavedSearchRunning\":false}",
            };
            return GetListingsFor(requests);
        }

        public static IList<Listing> GetListingsForLocations(DateTime date)
        {
            string strDate = date.Month + "/" + date.Day + "/" + date.Year;
            string[] requests = 
            { 
                // Ahuntsic
                "{\"maximumResults\":100,\"searchCriteria\":{\"Source\":\"FCIQ\",\"CurrentTab\":\"List\",\"EncryptID\":\"2is5maaKHd8=\",\"iMapZoom\":14,\"MapCoord\":\"45.58070666643242, -73.70461463928224,45.52234525665671, -73.62625122070312\",\"County\":\"%GS4621\",\"Address\":\"\",\"Area\":\"\",\"MLSArea\":\",840\",\"School\":\"\",\"City\":\"\",\"Neighborhood\":\"\",\"State\":\"\",\"Zipcode\":\"\",\"SrchType\":\"\",\"PropType\":\"\",\"iMinPrice\":0,\"iMaxPrice\":0,\"iBeds\":3,\"iBaths\":0,\"iBathsFull\":0,\"iBathsPartial\":0,\"iNumStories\":0,\"iSqFtMin\":0,\"iSqFtMax\":0,\"fAcreMin\":0,\"fAcreMax\":0,\"iYrBuiltMin\":0,\"iYrBuiltMax\":0,\"Pool\":\"\",\"Waterfront\":\"\",\"Waterview\":\"\",\"ShortSaleREO\":\"\",\"iGarage\":0,\"Search1\":\"\",\"Search2\":\"\",\"Search3\":\"\",\"Search4\":\"\",\"Search5\":\"\",\"Search6\":\"\",\"Search7\":\"\",\"Search8\":\"\",\"AllRemarks\":\"\",\"AllFeatures\":\"\",\"OpenHouse\":\"\",\"HasVirtualTour\":\"\",\"HasPhoto\":\"\",\"Status\":\"A\",\"isRentalOnlySearch\":\"Y\",\"UserHomeMLS\":\"FCIQ\",\"Priority\":\"\",\"AgentID\":\"\",\"isQuickCountable\":false,\"ListedSinceDate\":\"" + strDate + "\"},\"isSavedSearchRunning\":false}",
                // St-Laurent
                "{\"maximumResults\":100,\"searchCriteria\":{\"Source\":\"FCIQ\",\"CurrentTab\":\"List\",\"EncryptID\":\"2is5maaKHd8=\",\"iMapZoom\":15,\"MapCoord\":\"45.5298015824607, -73.6996364593506,45.50060202102101, -73.66045475006104\",\"County\":\"%GS4621\",\"Address\":\"\",\"Area\":\"\",\"MLSArea\":\",827\",\"School\":\"\",\"City\":\"\",\"Neighborhood\":\"\",\"State\":\"\",\"Zipcode\":\"\",\"SrchType\":\"\",\"PropType\":\"\",\"iMinPrice\":0,\"iMaxPrice\":0,\"iBeds\":3,\"iBaths\":0,\"iBathsFull\":0,\"iBathsPartial\":0,\"iNumStories\":0,\"iSqFtMin\":0,\"iSqFtMax\":0,\"fAcreMin\":0,\"fAcreMax\":0,\"iYrBuiltMin\":0,\"iYrBuiltMax\":0,\"Pool\":\"\",\"Waterfront\":\"\",\"Waterview\":\"\",\"ShortSaleREO\":\"\",\"iGarage\":0,\"Search1\":\"\",\"Search2\":\"\",\"Search3\":\"\",\"Search4\":\"\",\"Search5\":\"\",\"Search6\":\"\",\"Search7\":\"\",\"Search8\":\"\",\"AllRemarks\":\"\",\"AllFeatures\":\"\",\"OpenHouse\":\"\",\"HasVirtualTour\":\"\",\"HasPhoto\":\"\",\"Status\":\"A\",\"isRentalOnlySearch\":\"Y\",\"UserHomeMLS\":\"FCIQ\",\"Priority\":\"\",\"AgentID\":\"\",\"isQuickCountable\":false,\"ListedSinceDate\":\"" + strDate + "\"},\"isSavedSearchRunning\":false}"
            };
            return GetListingsFor(requests);
        }

        public static IList<Listing> GetListingsFor(string[] requests)
        {
            var listings = new List<Listing>();

            foreach (var r in requests)
            {
                var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://search.centris.ca/MyAjaxService.asmx/PropertySearch");
                httpWebRequest.ContentType = "application/json";
                httpWebRequest.Method = "POST";

                using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
                {
                    streamWriter.Write(r);
                }

                var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    var responseText = streamReader.ReadToEnd();
                    if (responseText.Length > 10)
                    {
                        // parse
                        responseText = responseText.Remove(0, 7);
                        responseText = responseText.Remove(responseText.Length - 3, 3);

                        var fulllist = responseText.Split(new[] { "\",\"" }, StringSplitOptions.None);
                        foreach (var item in fulllist)
                        {
                            var listingDetails = item.Split(new[] { "\\n" }, StringSplitOptions.None);
                            var listing = new Listing()
                            {
                                RawData = item,
                                Latitude = listingDetails[0],
                                Longitude = listingDetails[1],
                                Image = listingDetails[4],
                                Adresse = listingDetails[5],
                                NoMLS = listingDetails[3],
                                Chambres = listingDetails[8],
                                SallesDeBain = listingDetails[9],
                                DimensionTerrain = listingDetails[10],
                                AnneeConstruction = listingDetails[12],
                                Prix = listingDetails[6],
                                EvaluationMunicipale = listingDetails[19],
                                Ville = listingDetails[23],
                                CodePostal = listingDetails[24],
                                DateAjout = listingDetails[73],
                                Agence = listingDetails[69]
                            };
                            listings.Add(listing);
                        }
                    }
                }
            }
            return listings;
        }
    }
}
